package manager;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import java.io.File;

import longrise.phone.com.mylibrary.R;
import widget.AlertDialogRecord;

public class RecordViewManager {

    private Context context;

    private String recordPath;

    private RecordHelper recordHelper;// 录音帮助类

    private AnimationDrawable anim;//录音播放动画

    private View play;//播放按钮
    private View record;//录音按钮
    private AlertDialogRecord alertDialogRecord;

    public void setOpenAnim(boolean openAnim) {
        this.openAnim = openAnim;
    }

    private boolean openAnim=true;

    /**
     *
     * @param context
     * @param record 录制View
     * @param play  播放View
     */
    public RecordViewManager(Context context, View record,View play) {
        this.context = context;
        this.record = record;
        this.play = play;
        initViews();
    }

    private void initViews() {
        if(play.getBackground()==null) play.setBackgroundResource(R.mipmap.sound1);
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    play.setVisibility(View.GONE);
                    if (null!=recordHelper&&recordHelper.isPlaying()) {
                        recordHelper.stopMPlayer();
                    }
                    String recordPath = getDataFilePath(".mp3");
                    if (recordPath != null) {
                        File file = new File(recordPath).getParentFile();
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        if (recordHelper == null) {
                            initRecord();
                        }
                        recordHelper.startRecord(recordPath);
                        showRecordPopWindow();
                    } else {
                        Toast.makeText(context, "内存卡不存在或已损坏！", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recordHelper == null) {
                    initRecord();
                }
                if (!recordHelper.isPlaying()) {
                    if (openAnim) showPlayAnim();
                }
                recordHelper.playRecord(recordPath);
            }
        });

        if (play == null ) {
            return;
        }
        if (TextUtils.isEmpty(recordPath)) {
            play.setVisibility(View.GONE);
        }else {//录音完成，将播放图像显示
            play.setVisibility(View.VISIBLE);
        }
    }

    public recordOver getMyRecordOver() {
        return myRecordOver;
    }

    public void setMyRecordOver(recordOver myRecordOver) {
        this.myRecordOver = myRecordOver;
    }

    private recordOver myRecordOver;

    public interface recordOver {
        void onRecordOver();
    }

    // 实例化录音帮助类
    private void initRecord() {
        recordHelper = RecordHelper.getInstance(context);
        recordHelper.setmRecordOver(myRecordOver);
        recordHelper.setMyPlayIV(play);
        recordHelper.setMyrecord_iv_record(record);
        recordHelper.setOnRecordFinishListener(new RecordHelper.onRecordFinishListener() {
            @Override
            public void recordFinish(int time, String path) {
                closeRecordPopWindow();
                if ( time < 2) {
                    new File(path).delete();
                    if (time < 2) {
                        Toast.makeText(context, "录音时间太短!", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
                if (!TextUtils.isEmpty(path)) {
                    recordPath = path;
                    play.setVisibility(View.VISIBLE);
                }
            }
        });
        recordHelper.setOnPlayFinishListener(new RecordHelper.onPlayFinishListener() {
            @Override
            public void playFinish() {
                // ֹͣ停止播放动画
                if (anim != null) {
                    anim.stop();
                    play.setBackgroundResource(R.mipmap.sound1);
                }
            }
        });
    }

    /**
     * 显示播放的动画效果
     */
    private void showPlayAnim() {
        //play.setImageResource(R.drawable.record_voice_playanim);
       // anim = (AnimationDrawable) play.getDrawable();
        play.setBackgroundResource(R.drawable.record_voice_playanim);
        anim = (AnimationDrawable) play.getBackground();
        anim.start();
    }

    /**
     * 显示录音的悬浮框
     */
    private void showRecordPopWindow() throws Exception {
        alertDialogRecord = new AlertDialogRecord(context).builder();
        alertDialogRecord.setRecordOver(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordHelper.finishRecord();
            }
        });
        alertDialogRecord.show();
    }

    /**
     * 关闭录音的悬浮框
     */
    private void closeRecordPopWindow() {
        if(null!=alertDialogRecord){
            alertDialogRecord.dismiss();
        }
        alertDialogRecord=null;
    }

    public String getRecordPath() {
        return recordPath;
    }

    /**
     * 获取录音文件的本地路径
     */
    public String getDataFilePath(String fileSuffix) {
        if (!Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append(Environment.getExternalStorageDirectory());
        buffer.append("/");
        buffer.append("ly_record");
        buffer.append("/");
        buffer.append("data");
        buffer.append("/");
        buffer.append(System.currentTimeMillis());
        if (!TextUtils.isEmpty(fileSuffix)) {
            buffer.append(fileSuffix);
        }
        return buffer.toString();
    }
}
